package com.dg.cuckoo.admin.service.impl;

import com.dg.cuckoo.admin.dto.projectDeclare.oneProDeclare.EarlyDeclareDto;
import com.dg.cuckoo.admin.repository.EarlyDeclareRepository;
import com.dg.cuckoo.admin.repository.FileRepository;
import com.dg.cuckoo.admin.service.EarlyDeclareService;
import com.dg.cuckoo.admin.service.FileService;
import com.dg.cuckoo.admin.service.ProjectInfoServer;
import com.dg.cuckoo.common.utils.PageModel;
import com.dg.cuckoo.core.ServiceException;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.List;

@Service
public class EarlyDeclareServiceimpl implements EarlyDeclareService {

    @Autowired
    private EarlyDeclareRepository repository;
    @Autowired
    private ProjectInfoServer projectInfoServer;
    @Autowired
    private FileRepository fileRepository;
    @Autowired
    private FileService fileService;

    @Override
    public void save(EarlyDeclareDto dto, MultipartFile[] files) throws Exception {
        dto=checkSave(dto);
        EarlyDeclareDto save = repository.save(dto);
        if(files!=null){
            projectInfoServer.powerfulFileSave(files,save.getId());
        }
    }

    @Override
    public void delect(String id) {
        if(id==null){
            throw new ServiceException("请选择行");
        }
        repository.deleteById(id);
        fileRepository.deleteAll(fileRepository.findByByMark(id));
    }

    @Override
    public String openInfo(String id) {
        if(id==null){
            throw new ServiceException("请选择行");
        }
        EarlyDeclareDto dto = repository.findById(id).get();
        List<String> files = fileService.byByMark(id);
        JSONObject json = new JSONObject();
        json.put("dto",dto);
        json.put("files",files);
        return json.toString();
    }

    @Override
    public PageModel page(Integer page, Integer pageSize) {
        Integer one = page;
        page = (page - 1) * pageSize;
        int size = repository.findAll().size();
        List<EarlyDeclareDto> dtos = repository.page(page, pageSize);
        return new PageModel(one,pageSize,size,dtos);
    }

    @Override
    public void update(String id, EarlyDeclareDto dto, MultipartFile[] files) throws Exception {
        if(id==null){
            throw new ServiceException("请选择行");
        }
//        dto=checkSave(dto);
        dto.setId(id);

        EarlyDeclareDto save = repository.save(dto);
        fileRepository.deleteAll(fileRepository.findByByMark(id));
        if(files!=null){
            projectInfoServer.powerfulFileSave(files,save.getId());
        }
    }


    public EarlyDeclareDto checkSave(EarlyDeclareDto dto){
        if(dto.getProjectName()==null){
            throw new ServiceException("请选择项目名称");
        }
        if(repository.findByProjectName(dto.getProjectName())!=null){
            throw new ServiceException("项目名称已存在");
        }
//        dto.setTotalAmount(new BigDecimal(dto.getCityFinancial()).add(new BigDecimal(dto.getTownFinancial())).add(new BigDecimal(dto.getSuperiorSubsidy())).add(new BigDecimal(dto.getFinancing())).add(new BigDecimal(dto.getSelfFunded())).toString());
        return dto;
    }
}
